package com.mspbots.rule.exceptions.cw;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.mspbots.api.service.TenantUserTimeEntryService;
import com.mspbots.cw.request.TenantTimeEntryDTO;
import com.mspbots.rule.exceptions.BaseRule;
import lombok.extern.slf4j.Slf4j;
import org.jeasy.rules.annotation.Action;
import org.jeasy.rules.annotation.Fact;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;


/**
 * com.mspbots.rule.exceptions.timeentry.CwBaseRule
 *
 * @author Jun Zhou
 * @date 9:44 2019/7/30
 **/
@Slf4j
@Component
public abstract class CwBaseRule extends BaseRule implements IcwBaseRule {

    protected TenantUserTimeEntryService tenantUserTimeEntryService;

    public CwBaseRule() {
    }


    protected StringBuilder formatCwLink(Long cwTid) {
        StringBuilder messageBuilder = new StringBuilder();
        messageBuilder.append(
                "<a href='https://" + getConfiguration(KEYWORD_WISE_SITE) + "/v4_6_release/ConnectWise.aspx?routeTo=TimeEntryFV&recid=");
        messageBuilder.append(cwTid).append("'>");
        messageBuilder.append(cwTid).append("</a>");
        return messageBuilder;
    }

    protected StringBuilder suffixCwLink(Long cwTid) {
        return new StringBuilder().append("[").append(formatCwLink(cwTid)).append("]&nbsp;");
    }


    protected StringBuilder suffixMessage(Long cwTid, LocalDateTime lastModified) {
        return new StringBuilder().append("--<span style='color:#999d9c;font-size:10px;'>")
                .append(suffixUserName())
//                .append(suffixCwLink(cwTid))
                .append(suffixCwZoneTime(lastModified))
                .append("</span>");

    }

    /**
     * has record : true
     *
     * @param entry
     * @return
     */
    protected boolean checkMessage(TenantTimeEntryDTO entry) {
        messages.setTimeEntryId(entry.getCwTid());
        return super.checkMessage(messages);
    }

    protected Flux<JsonNode> hasMessage(TenantTimeEntryDTO entry) {
        messages.setTimeEntryId(entry.getCwTid());
        return super.hasMessage(messages);
    }

    protected boolean insertMessage() {
        messages.setBusinessType("time_entry");
        return super.insertMessage(messages);
    }


    @Action
    @Async
    @Override
    public void action(@Fact(FACT) TenantTimeEntryDTO entry) {
        String message = getMessage(entry);
        if (message != null) {
            messages.setMessage(message);
            messages.setTimeEntryId(entry.getCwTid());
            messages.setBusinessId(entry.getCwTid());
            insertMessage();
        }


    }


    protected String getMessage(TenantTimeEntryDTO entry) {
        if (tenantUserMappingDTO == null) {
            log.error("tenant_user_id not mapping teams user [{}]", entry.getTenantUserId());
            return null;
        }
        StringBuilder msg = formatMessage(true);
        msg.append(suffixMessage(entry.getCwTid(), entry.getEnteredDate()));
        Map<String, Object> params = new HashMap<>();
        params.put("TimeEntryLink", formatCwLink(entry.getCwTid()));
        return StrUtil.format(msg, params);
    }

    public void setTenantUserTimeEntryService(TenantUserTimeEntryService tenantUserTimeEntryService) {
        this.tenantUserTimeEntryService = tenantUserTimeEntryService;
    }

}
